package com.pomelo.base.core.datasource;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.pomelo.base.core.config.PomeloTransactionStatus;

/**
 * 动态数据源上下文
 *
 * @author biankudingcha
 * @date: 2021-07-23
 * @since 0.5.0
 */
public class DynamicDataSourceContextHolder {

    /**
     * 上下文线程
     */
    private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<String>();

    /**
     * 数据源Id列表
     */
    private static final List<String> DATASOURCE_IDS = new ArrayList<String>();

    /**
     * 事务状态上下文
     */
    private static final ThreadLocal<Map<String, PomeloTransactionStatus>> TRANSACTION_STATUS = new ThreadLocal<Map<String, PomeloTransactionStatus>>();

    /**
     * 设置当前数据源id
     *
     * @param dataSourceId 数据源id
     */
    public static void setDataSourceId(String dataSourceId) {
        if (dataSourceId == null || "".equals(dataSourceId)) {
            return;
        }
        CONTEXT_HOLDER.set(dataSourceId);
    }

    /**
     * 获取当前数据源id
     *
     * @return String
     */
    public static String getDataSourceId() {
        return CONTEXT_HOLDER.get();
    }

    /**
     * 清楚当前数据源
     */
    public static void clearDataSourceId() {
        CONTEXT_HOLDER.remove();
    }

    /**
     * 设置数据源事务
     *
     * @param key 数据源id
     * @param transactionStatus 事务状态
     */
    public static void setTransactionStatu(String key, PomeloTransactionStatus transactionStatus) {
        if (TRANSACTION_STATUS.get() == null) {
            Map<String, PomeloTransactionStatus> transactionStatusMap = new LinkedHashMap<>();
            TRANSACTION_STATUS.set(transactionStatusMap);
        }
        TRANSACTION_STATUS.get().put(key, transactionStatus);
    }

    /**
     * 获取事务状态
     *
     * @param key 数据源id
     * @return TransactionStatus
     */
    public static PomeloTransactionStatus getTransactionStatu(String key) {
        return TRANSACTION_STATUS.get() == null ? null : TRANSACTION_STATUS.get().get(key);
    }

    /**
     * 移除事务状态
     *
     * @param key 数据源id
     */
    public static void removeTransactionStatu(String key) {
        if (TRANSACTION_STATUS.get() != null) {
            TRANSACTION_STATUS.get().remove(key);
        }
    }

    /**
     * 获取所有事务状态
     *
     * @return Map
     */
    public static Map<String, PomeloTransactionStatus> getTransactionStatus() {
        return TRANSACTION_STATUS.get() == null ? null : TRANSACTION_STATUS.get();
    }

    /**
     * 清空事务状态
     */
    public static void clearTransactionStatu() {
        TRANSACTION_STATUS.remove();
    }

    /**
     * 判断数据源id是否存在
     *
     * @param dataSourceId 数据源id
     * @return boolean
     */
    public static boolean containsDataSourceId(String dataSourceId) {
        return DATASOURCE_IDS.contains(dataSourceId);
    }

    /**
     * 获取所有数据源id
     *
     * @return List
     */
    public static List<String> getDataSourceIds() {
        return DATASOURCE_IDS;
    }
}
